package cn.test;

import com.iflytek.cloud.speech.*;

/**
 * 语音合成类
 * 1.合成到播放， 2.合成到文件
 */
public class Synthesizer {

    private SpeechSynthesizer mTts;
    private static Synthesizer synthesizer = null;

    private Synthesizer(){
        SpeechUtility.createUtility(SpeechConstant.APPID + "=" + Config.APPID);
        mTts = SpeechSynthesizer.createSynthesizer();
        mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");//设置发音人
        mTts.setParameter(SpeechConstant.SPEED, "50");//设置语速
        mTts.setParameter(SpeechConstant.PITCH, "50");
        mTts.setParameter(SpeechConstant.VOLUME, "80");//设置音量，范围0~100
    }

    public static Synthesizer getInstance(){
        if(synthesizer == null){
            synthesizer = new Synthesizer();
        }
        return synthesizer;
    }

    //合成到文件监听器
    private SynthesizeToUriListener synthesizeToUriListener = new SynthesizeToUriListener() {
        @Override
        public void onBufferProgress(int i) {

        }

        @Override
        public void onSynthesizeCompleted(String s, SpeechError speechError) {
            System.out.println("onSynthesizeCompleted");
            App.status = "synthesizerCompleted";
        }

        @Override
        public void onEvent(int i, int i1, int i2, int i3, Object o, Object o1) {

        }
    };

    //合成到播放监听器
    private SynthesizerListener mSynListener = new SynthesizerListener() {
        @Override
        public void onBufferProgress(int i, int i1, int i2, String s) {
            System.out.println(s);
        }

        @Override
        public void onSpeakBegin() {

        }

        @Override
        public void onSpeakProgress(int i, int i1, int i2) {

        }

        @Override
        public void onSpeakPaused() {

        }

        @Override
        public void onSpeakResumed() {

        }

        @Override
        public void onCompleted(SpeechError speechError) {
            System.out.println("synthesizerCompleted");
            App.status = "synthesizerCompleted";
        }

        @Override
        public void onEvent(int i, int i1, int i2, int i3, Object o, Object o1) {

        }
    };

    /*
     * 合成到文件
     */
    public void synthesizerToFile(String text, String path2VoiceFile){
        //3.开始合成
        //设置合成音频保存位置（可自定义保存位置），默认保存在"./tts_test.pcm"
        mTts.synthesizeToUri(text, path2VoiceFile, synthesizeToUriListener);
    }

    /*
     * 合成到播放
     */
    public void systhesizerToVoice(String text, String path2VoiceFile){
        //设置合成音频保存位置（可自定义保存位置），保存在“./tts_test.pcm”
        //如果不需要保存合成音频，注释该行代码
        mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, path2VoiceFile);
        //开始合成
        mTts.startSpeaking(text, mSynListener);
    }

    public static void main(String[] args) {

        Synthesizer app = getInstance();
        //合成到文件
        //app.synthesizerToFile(Config.TEXT2BESYNTHESIZER, Config.VOICEFILE);
        //合成到播放,合成到播放也设置了保存至文件
        app.systhesizerToVoice(Config.TEXT2BESYNTHESIZER, Config.VOICEFILE);
    }

}
